<?php
/**
 * Description of Class_Private_Message
 *
 * @author Fantastik78
 */

class PrivateMessage {

    private $subject = "subject";
    private $content = "content";
    private $receiver = "receiver";
    private $trim_content;
    private $final_subject;

    //ERRORS
        //sendMessage Function
        private $e_content_empty = false;
        private $e_receiver_empty = false;

    public function getErrorsSendMessage(){
        if(isset($_POST['btn_send_msg'])){
            if($this->e_content_empty){ echo "Le contenu du message ne doit pas être vide.";}
            if($this->e_receiver_empty){ echo "Le membre que vous souhaitez contacter n'existe pas.";}
        }
    }

    public function sendMessage(){
        if(isset($_POST['btn_send_msg'])){

            $post_secu = array_map("mysql_real_escape_string", $_POST);
            if(!empty($post_secu[$this->content])){
                $this->trim_content = trim($post_secu[$this->content]);
                if($this->trim_content != null){
                    if(!empty($post_secu[$this->receiver])){
                        $query = mysql_query("SELECT id_member FROM member WHERE pseudo='" . $post_secu[$this->receiver] . "'");
                        $member = mysql_fetch_array($query);
                        if(empty($post_secu[$this->subject])){ $this->final_subject = ""; }else{ $this->final_subject = $post_secu[$this->subject]; }
                        $query_ins = mysql_query("INSERT INTO private_message (sender, receiver, subject, content) VALUES ('" . $_SESSION['member_session']['id'] .  "', '" . $member['id_member'] . "', '" . $this->final_subject . "', '" . $this->trim_content . "')");
                    }else{
                        $this->e_receiver_empty = true;
                    }
                }else{
                    $this->e_content_empty = true;
                }
            }else{
                $this->e_content_empty = true;
            }
        }
    }


    public function receiveMessage(){

        $query = mysql_query("SELECT * FROM private_message WHERE receiver='" . $_SESSION['member_session']['id'] . "'");
        echo "<table><caption>Received Message</caption>";
        while($result_query = mysql_fetch_array($query)){

            $match_pseudo = mysql_query("SELECT pseudo FROM member WHERE id_member='" . $result_query['sender'] . "'");
            $res_match_pseudo = mysql_fetch_array($match_pseudo);

            echo "<tr>";
            echo "<th><input type=\"checkbox\" name=\"check[]\" value=\"" . $result_query['id'] . "\"/></th>";
            echo "<th>" . $res_match_pseudo['pseudo'] . "</th>";
            echo "<th>" . $result_query['subject'] . "</th>";
            echo "<th>" . '<a href="'.ROOT.'/view-message?message_id=' . $result_query['id'] . '"><img src="../ressources/images/eye.png" /></a>' . "</th>";
            echo "</tr>";
        }
        echo "</table>";
    }

    public function viewMessage(){
        $query_msg = mysql_query("SELECT * FROM private_message WHERE id='" . $_GET['message_id'] . "'");
        $res_qr_msg = mysql_fetch_array($query_msg);

        $query_name = mysql_query("SELECT pseudo FROM member WHERE id_member='" . $res_qr_msg['sender'] . "'");
        $res_qr_name = mysql_fetch_array($query_name);
        
        $message = array ( 'sender' => $res_qr_name['pseudo'] ,'subject' => $res_qr_msg['subject'], 'content' => $res_qr_msg['content']);
        
        $query_status = mysql_query("UPDATE private_message SET status='check' WHERE id=" . $_GET['message_id'] . "");

        return $message;
    }

    public function deleteMultipleMessage(){
        if(isset($_POST['btn_del_check'])){
            if(isset($_POST['check'])){
                foreach($_POST['check'] as $key=>$value){
                    mysql_query("DELETE FROM private_message WHERE id='" . $value . "'");
                }
            }
        }
    }

}

?>
